import { createRouter, createWebHistory } from "vue-router";
import loginView from "@/views/login/login.vue";
import shouyeView from "@/views/login/shouye.vue";
import fenleiView from "@/views/login/fenlei.vue";
import gouwucheView from "@/views/login/gouwuche.vue";
import myView from "@/views/login/my.vue";
import dlView from "@/views/denglu/denglu.vue";
import zcView from "@/views/zhuce/zhuce.vue";
import lbView from "@/views/liebiao/lb.vue";
import xqView from "@/views/xiangqing/xq.vue";
import orderView from "@/views/order/order.vue";
import myorderView from "@/views/myorder/myorder.vue";
import wlView from "@/views/wl/wl.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/login/shouye" },
    {
      path: "/login",
      component: loginView,
      children: [
        { path: "shouye", component: shouyeView },
        { path: "fenlei", component: fenleiView },
        { path: "gouwuche", component: gouwucheView },
        { path: "my", component: myView },
      ],
    },
    { path: "/denglu", component: dlView },
    { path: "/zhuce", component: zcView },
    { path: "/lb/:cat_name", component: lbView },
    { path: "/xq/:goods_id", component: xqView },
    { path: "/order/:orderId", component: orderView },
    { path: "/myorder", component: myorderView },
    { path: "/wl", component: wlView },
  ],
});
router.beforeEach((to, from, next) => {
  if (to.fullPath == "/login/my"||to.fullPath == "/login/gouwuche") {
    let token = localStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/denglu");
    }
  } else {
    next();
  }
});
export default router;
